package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "用户管理模块")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @Operation(summary = "分页")
    @PostMapping("/findPage/{pageNum}/{pageSize}")
    public Result<PageInfo<SysUser>> findPage(@RequestBody SysUserDto sysUserDto,
                                              @PathVariable("pageNum") Integer pageNum,
                                              @PathVariable("pageSize") Integer pageSize){
        PageInfo<SysUser> pageInfo =  sysUserService.findPage(sysUserDto,pageNum,pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }




    @Operation(summary = "保存")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser){
        sysUserService.save(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据ID查询角色")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Long id){
        SysUser sysUser = sysUserService.getById(id);
        return Result.build(sysUser,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "修改")
    @PutMapping("/update")
    public Result update(@RequestBody SysUser sysUser){
        sysUserService.update(sysUser);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据id删除角色")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        sysUserService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }



    @Operation(summary = "根据用户id查询用户角色")
    @GetMapping("/findRoleByUserId/{id}")
    public Result<Map<String,Object>> findRoleByUserId(@PathVariable("id") Long id){
        //Map中封装两个部分数据，1-查询所有角色集合   2-这个用户拥有的角色id集合
        Map<String,Object> data = sysUserService.findRoleByUserId(id);
        return Result.build(data,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "给用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto){
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }













}
